/*
 * Licensed to the 南京汇海交通科技有限公司
 *
 * Copyright (c) 2025.
 * All rights reserved.
 *
 * 本软件属南京汇海交通科技有限公司所有，在未获得南京汇海交通科技有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.chframework.common.thread.wrapper;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * 该抽象类中重写的方法不主张被使用在项目中，
 * 在I/O密集型的应用中使用CompletableFuture时不指定非ForkJoinPool类型的线程池，结果极有可能适得其反。
 * ForkJoinPool类型的线程池适用于CPU密集型的应用中，批量处理耗时短的线程任务是它的长处！
 * 在基于Java Web构建的应用中，I/O密集型的线程任务占绝大多数，为了简化编码并减少出错的概率，
 * 有必要对CompletableFuture实现静态代理后再投入到实际项目中，并使用统一的ExecuteService线程池。
 *
 * @param <T> 异步响应结果的引用类型
 */
abstract class CompletableFutureAgent<T> implements Future<T>, CompletionStage<T> {

    CompletableFuture<T> completableFuture;

    CompletableFutureAgent(CompletableFuture<T> completableFuture) {
        this.completableFuture = completableFuture;
    }

    @Override
    @Deprecated
    public <U> BenchmarksFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.thenApplyAsync(fn, executor));
    }

    @Override
    @Deprecated
    public BenchmarksFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.thenAcceptAsync(action, executor));
    }

    @Override
    @Deprecated
    public BenchmarksFuture<Void> thenRunAsync(Runnable action, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.thenRunAsync(action, executor));
    }

    @Override
    @Deprecated
    public <U, V> BenchmarksFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.thenCombineAsync(other, fn, executor));
    }

    @Override
    @Deprecated
    public <U> BenchmarksFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.thenAcceptBothAsync(other, action, executor));
    }

    @Override
    @Deprecated
    public BenchmarksFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.runAfterBothAsync(other, action, executor));
    }

    @Override
    @Deprecated
    public <U> BenchmarksFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.applyToEitherAsync(other, fn, executor));
    }

    @Override
    @Deprecated
    public BenchmarksFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.acceptEitherAsync(other, action, executor));
    }

    @Override
    @Deprecated
    public BenchmarksFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.runAfterEitherAsync(other, action, executor));
    }

    @Override
    @Deprecated
    public <U> BenchmarksFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.thenComposeAsync(fn, executor));
    }

    @Override
    @Deprecated
    public BenchmarksFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.whenCompleteAsync(action, executor));
    }

    @Override
    @Deprecated
    public <U> BenchmarksFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) {
        return new BenchmarksFuture<>(this.completableFuture.handleAsync(fn, executor));
    }

}
